var requestService = require('../../utils/requestService.js');
var app = getApp();
Page({
  data: {
    address:'',//桩地址
    qrCode:'',//二维码编号
    startBt:{
      disabled: false,
      loading: false
    }
   },
  onLoad: function (options) {
    // 设置本页导航标题
    wx.setNavigationBarTitle({
      title: '开启充电'
    })
    const that = this;
    const qrCode=options.qrCode;
   requestService.sendGetRequest("/charger/"+qrCode+"/station",null,function(result){
          const resultData=result.data;
          if(resultData.resCode==0){
              const stationInfo=resultData.resData;
              that.setData({
                  qrCode:qrCode,
                  address:stationInfo.parkingAddress
              })
          }else{
              that.setData({
                  "startBt.disabled":true
              })
          }
      });
  },
    /**请求服务器开始充电*/
  startCharging: function (){
        const that=this;
        that.setData({
            "startBt.disabled":true,
            "startBt.loading":true
        })
        const qrCode=this.data.qrCode;
        requestService.sendPostRequest("/charger/scanCharge",{'qrCode':qrCode},function(result){
            that.setData({
                "startBt.disabled":false,
                "startBt.loading":false
            })
            const resultData=result.data;
            if(resultData.resCode==0){
                wx.redirectTo({
                    url: '../charge/index?address='+that.data.address
                })
            }else{
                wx.showModal({
                    title: '提示',
                    content: resultData.message,
                    showCancel: false
                })
            }
        },function(result){
            that.setData({
                "startBt.disabled":false,
                "startBt.loading":false
            })
            wx.showModal({
                title: '提示',
                content: '失败',
                showCancel: false
            })
        })
    }

})